package nfc;

import com.main.easycare.MainActivity;
import com.main.easycare.MenuActivity;
import com.main.easycare.R;
import com.main.easycare.R.layout;
import com.main.easycare.R.menu;

import android.app.Activity;
import android.app.PendingIntent;
import android.content.Intent;
import android.content.IntentFilter;
import android.nfc.NfcAdapter;
import android.nfc.Tag;
import android.os.Bundle;
import android.view.Menu;

public class IdentificationActivity extends Activity {

	/*
	 * Declaration des variables
	 */

	private NfcAdapter adapter_nfc;
	private PendingIntent nfcPendingIntent;
	private Tag tag;
	private IntentFilter writeTagFilters[];


	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_identification);
		adapter_nfc = NfcAdapter.getDefaultAdapter(this);
		if(adapter_nfc == null || !adapter_nfc.isEnabled()){
			finish();
			return;
		}

		nfcPendingIntent = PendingIntent.getActivity(this, 0, new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
		IntentFilter tagDetected = new IntentFilter(NfcAdapter.ACTION_TAG_DISCOVERED);
		tagDetected.addCategory(Intent.CATEGORY_DEFAULT);
		writeTagFilters = new IntentFilter[] { tagDetected };
	}


	// State : RESUME
	public void onResume()
	{
		super.onResume();
		Intent intent = new Intent(this,this.getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
		nfcPendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
		IntentFilter[] filters= null;
		String[][] techListArray =null;
		adapter_nfc.enableForegroundDispatch(this, nfcPendingIntent, filters, techListArray);
	}
	//


	//surcharge de fct, qd on detecte tag -> on rentre dedans
	protected void onNewIntent(Intent intent){ 
		//tvTag.setText("Dans le onNewIntent");
		if(NfcAdapter.ACTION_TAG_DISCOVERED.equals(intent.getAction()) 
				|| NfcAdapter.ACTION_NDEF_DISCOVERED.equals(intent.getAction()) 
				|| NfcAdapter.ACTION_TECH_DISCOVERED.equals(intent.getAction())){
			tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
			//tvTag.setText("Tag normalement bon !");

			Intent intent2 = new Intent(IdentificationActivity.this, MenuActivity.class);
			startActivityForResult(intent2,0);
			// TODO fonction verification medecin
		}
	}
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.identification, menu);
		return true;
	}

}
